#!/bin/sh
USER_NAME=BEEcube
SELINUX_CONFIG="/etc/sysconfig/selinux"
VNCSERVER_CONFIG="/etc/sysconfig/vncservers"
SAMBA_CONFIG="/etc/samba/smb.conf"
BASHRC="/home/$USER_NAME/.bashrc"

unalias -a
echo
echo "Install tools and config system, including:"
echo "	1. iptables"
echo "	2. minicom"
echo "	3. vncserver"
echo "	4. samba"
echo "	5. SELINUX"

# Make sure the user has superuser (root) privileges
if (( $EUID != 0 )); then
	echo "superuser (root) privileges are required for installation"
	exit 22
fi

#Disable firewall
echo "disable firewall"
	service iptables stop
	chkconfig iptables off
#Disable SELINUX
echo "disable SELINUX"
	setenforce 0
	sed -i -e "s|SELINUX=enforcing|SELINUX=disabled|" $SELINUX_CONFIG

# Check minicom have been installed.
CMD_PATH=`which minicom 2>/dev/null`
if [ $? -ne 0 ]; then
	echo "minicom have not been installed yet!"
	echo "YUM to install..."
 	yum -y install minicom	
else
	echo "minicom have beed installed... OK"
fi

# Check vncserver have been installed.
CMD_PATH=`which vncserver 2>/dev/null`
if [ $? -ne 0 ]; then
	echo "vncserver have not been installed yet!"
	echo "YUM to install..."
	yum -y install tigervnc
else
	echo "vncserver have beed installed... OK"
	echo "Configure vncserver..."
	chkconfig vncserver on
	grep $USER_NAME $VNCSERVER_CONFIG
	if [ $? -ne 0 ]; then
		echo "VNCSERVERS=\"1:BEEcube\"" >> $VNCSERVER_CONFIG 
		echo "VNCSERVERARGS[1]=\"-geometry 1400x1050\"" >> $VNCSERVER_CONFIG
	fi
	service vncserver restart
	sed -i -e "s|twm|gnome-session|" /home/$USER_NAME/.vnc/xstartup
fi

 Check SAMBA have been installed
CMD_PATH=`which smbd 2>/dev/null`
if [ $? -ne 0 ]; then
	echo "samba service have not been installed yet!"
	echo "YUM to install..."
	yum -y install samba
else
	echo "samba have been installed... OK"
	echo "Configure samba..."
	echo "Add $USER_NAME as SAMBA user"
	smbpasswd -a $USER_NAME
	chkconfig smb on
	grep $USER_NAME $SAMBA_CONFIG > /dev/null
	if [ $? -ne 0 ]; then
		echo "[$USER_NAME]" >> $SAMBA_CONFIG 
		echo "	comment = $USER_NAME" >> $SAMBA_CONFIG 
		echo "	path = /home/$USER_NAME" >> $SAMBA_CONFIG
		echo "	public = yes" >> $SAMBA_CONFIG
		echo "	writeable = yes" >> $SAMBA_CONFIG
		echo "	veto files =/.*/" >> $SAMBA_CONFIG
	fi
	service smb  start
fi

# Patch user bashrc file
grep $USER_NAME $BASHRC > /dev/null
if [ $? -ne 0 ]; then
	echo "Patch $USER_NAME bashrc file"
	echo ". /opt/Xilinx/13.4/LabTools/settings64.sh >>/dev/null " >> $BASHRC
	echo ". /opt/BEEcube/settings.sh " >> $BASHRC
	echo "mBEE4Control -1 mBEE4" >> $BASHRC
else
	echo "Checking $USER_NAME bashrc... OK"
fi
